package com.ruoyi.service.controller;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.service.utils.ServiceUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.service.domain.Economize;
import com.ruoyi.service.service.IEconomizeService;
import com.ruoyi.framework.web.base.BaseController;
import com.ruoyi.common.page.TableDataInfo;
import com.ruoyi.common.base.AjaxResult;
import com.ruoyi.common.utils.ExcelUtil;

/**
 * 节能替代方案 信息操作处理
 * 
 * @author john
 * @date 2019-01-04
 */
@Controller
@RequestMapping("/service/economize")
public class EconomizeController extends BaseController
{
    private String prefix = "service/economize";
	
	@Autowired
	private IEconomizeService economizeService;
	@Autowired
	private ServiceUtils utils;
	
	@GetMapping()
	public String economize()
	{
	    return prefix + "/economize";
	}
	
	/**
	 * 查询节能替代方案列表
	 */
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Economize economize)
	{
		startPage();
        List<Economize> list = economizeService.selectEconomizeList(economize);
		return getDataTable(list);
	}
	
	
	/**
	 * 导出节能替代方案列表
	 */
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Economize economize)
    {
    	List<Economize> list = economizeService.selectEconomizeList(economize);
        ExcelUtil<Economize> util = new ExcelUtil<Economize>(Economize.class);
        return util.exportExcel(list, "economize");
    }
	
	/**
	 * 新增节能替代方案
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存节能替代方案
	 */
	@Log(title = "节能替代方案", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(Economize economize)
	{		
		return toAjax(economizeService.insertEconomize(economize));
	}

	/**
	 * 修改节能替代方案
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Integer id, ModelMap mmap)
	{
		Economize economize = economizeService.selectEconomizeById(id);
		mmap.put("month", utils.getMonth(economize.getBasicsId()));
		mmap.put("economize", economize);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存节能替代方案
	 */
	@Log(title = "节能替代方案", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(Economize economize)
	{
		BigDecimal fTotal = economize.getFF().add(economize.getFP().add(economize.getFG()));
		BigDecimal pTotal = economize.getPF().add(economize.getPP().add(economize.getPG()));
		BigDecimal kTotal = economize.getKF().add(economize.getKP().add(economize.getKG()));
//		电量合计
		BigDecimal eleTotal = fTotal.add(pTotal.add(kTotal));

		BigDecimal upAccount = eleTotal.multiply(economize.getFactoryPrice());
		BigDecimal sPAccount = eleTotal.multiply(economize.getSPPrice());
		BigDecimal jJAccount = eleTotal.multiply(economize.getJJAverage());
//		BigDecimal eleAccount = eleTotal.multiply(economize.getDHAverage());


		economize.setFTotal(fTotal);
		economize.setPTotal(pTotal);
		economize.setKTotal(kTotal);
		economize.setEleTotal(eleTotal);
		economize.setUpAccount(upAccount);
		economize.setSPAccount(sPAccount);
		economize.setJJAccount(jJAccount);
//		economize.setEleAccount(eleAccount);

//        用户承担电费金额eleAcount = 上网电费upAccount + 输配收入金额sPAccount
		economize.setEleAccount(economize.getUpAccount().add(economize.getSPAccount()));
//        到户均价dHAverage = 用户承担电费金额eleAcount / 留存电量合计keepTotal
		BigDecimal TradeTotal = economize.getEleTotal();
		if (TradeTotal == BigDecimal.valueOf(0)) {
			economize.setDHAverage(BigDecimal.ZERO);
		}else{
			economize.setDHAverage(economize.getEleAccount().divide(TradeTotal).setScale(6,BigDecimal.ROUND_HALF_UP));
		}

		int i = economizeService.updateEconomize(economize);
		utils.total(economize.getPointId(),economize.getBasicsId());
		return toAjax(i);
	}
	
	/**
	 * 删除节能替代方案
	 */
	@Log(title = "节能替代方案", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(economizeService.deleteEconomizeByIds(ids));
	}
	
}
